package com.sun.controller;

import com.alibaba.fastjson.JSON;
import com.sun.entity.Category;
import com.sun.service.CategoryService;
import jakarta.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author SunRui
 * @description TODO
 * @date 2024/8/10 22:44
 */
@Controller
@RequestMapping("/CategoryController")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    private static Logger logger = Logger.getLogger(BookController.class);
    private String jsonString;
    private String str;

    @RequestMapping("/homeUlLoad")
    public void homeUlLoad(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //列表数据加载
        List<Category> categoryList = categoryService.getList();
        logger.debug("列表数据:"+categoryList.toString());
        jsonString = JSON.toJSONString(categoryList);
        resp.getWriter().write(jsonString);
    }

    @RequestMapping("/categoryName")
    public void categoryName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id获取类型名称
        int id = Integer.parseInt(req.getParameter("categoryId"));
        String name = (String) categoryService.getName(id);
        jsonString = JSON.toJSONString(name);
        resp.getWriter().write(jsonString);
    }
}
